package com.fr.report.core.barcode.line.code39;

import com.fr.report.core.barcode.BarcodeException;
import com.fr.report.core.barcode.BarcodeImpl;
import com.fr.report.core.barcode.CompositeModule;
import com.fr.report.core.barcode.Module;
import com.fr.report.core.barcode.SeparatorModule;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.List;

public class Code39Barcode extends BarcodeImpl
{
  public static final String[] TYPES = { "Code39", "USD3", "3of9" };
  private boolean requiresChecksum;
  private String label;
  private int width;

  public Code39Barcode(String paramString, boolean paramBoolean)
    throws BarcodeException
  {
    this(paramString, paramBoolean, false);
  }

  public Code39Barcode(String paramString, boolean paramBoolean1, boolean paramBoolean2)
    throws BarcodeException
  {
    super((paramBoolean2) ? encodeExtendedChars(paramString) : validateBasicChars(paramString));
    this.requiresChecksum = false;
    this.width = 0;
    this.requiresChecksum = paramBoolean1;
    this.label = paramString;
  }

  protected String getLabel()
  {
    return "*" + this.label + "*";
  }

  protected double getBarcodeWidth(int paramInt)
  {
    encodeData();
    if (this.requiresChecksum)
      this.width += 1 + calculateChecksum().widthInBars();
    return (this.barWidth * this.width);
  }

  protected Module[] encodeData()
  {
    ArrayList localArrayList = new ArrayList();
    for (int i = 0; i < this.data.length(); ++i)
    {
      char c = this.data.charAt(i);
      localArrayList.add(new SeparatorModule(1));
      this.width += 1;
      Module localModule = ModuleFactory.getModule(String.valueOf(c));
      localArrayList.add(localModule);
      this.width += localModule.widthInBars();
    }
    localArrayList.add(new SeparatorModule(1));
    this.width += 1;
    return ((Module[])localArrayList.toArray(new Module[0]));
  }

  protected Module calculateChecksum()
  {
    if (this.requiresChecksum)
    {
      int i = calculateMod43(this.data);
      CompositeModule localCompositeModule = new CompositeModule();
      localCompositeModule.add(ModuleFactory.getModuleForIndex(i));
      localCompositeModule.add(new SeparatorModule(1));
      return localCompositeModule;
    }
    return null;
  }

  public static int calculateMod43(String paramString)
  {
    int i = 0;
    StringCharacterIterator localStringCharacterIterator = new StringCharacterIterator(paramString);
    for (int j = localStringCharacterIterator.first(); j != 65535; k = localStringCharacterIterator.next())
      i += ModuleFactory.getIndex(String.valueOf(j));
    int k = i % 43;
    return k;
  }

  protected Module getPreAmble()
  {
    return ModuleFactory.START_STOP;
  }

  protected Module getPostAmble()
  {
    return ModuleFactory.START_STOP;
  }

  private static String validateBasicChars(String paramString)
    throws BarcodeException
  {
    StringCharacterIterator localStringCharacterIterator = new StringCharacterIterator(paramString);
    int i = localStringCharacterIterator.first();
    while (i != 65535)
    {
      if (!(ModuleFactory.hasModule(String.valueOf(i), false)))
        throw new BarcodeException("Illegal character - try using extended mode if you need to encode the full ASCII character set");
      int j = localStringCharacterIterator.next();
    }
    return paramString;
  }

  private static String encodeExtendedChars(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    StringCharacterIterator localStringCharacterIterator = new StringCharacterIterator(paramString);
    int i = localStringCharacterIterator.first();
    while (i != 65535)
    {
      if (!(ModuleFactory.hasModule(String.valueOf(i), true)))
        localStringBuffer.append(ModuleFactory.getExtendedCharacter(i));
      else
        localStringBuffer.append(i);
      int j = localStringCharacterIterator.next();
    }
    return localStringBuffer.toString();
  }
}